package com.beaut.project.control;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import com.beaut.project.pojo.PageParameter;

import com.beaut.project.util.Utility;

@Component
@Aspect
public class PageAspect
{
 
    @Around("execution (* com.beaut.project.control..*.*Page*(..)) && args(request)")
    public String afterPageAspect(ProceedingJoinPoint  jp,HttpServletRequest request)
    {
    	int page = Integer.parseInt(request.getParameter("page"));
    	int pageSize = Integer.parseInt(request.getParameter("rows"));
    	Map<String,Object> map = new HashMap<String,Object>();
    	PageParameter p=new PageParameter();
    	p.setCurrentPage(page);
    	p.setPageSize(pageSize);
    	map.put("pageObject", p);
    	request.setAttribute("pageMap", map);
    	StringBuffer sb = new StringBuffer("");
    	String s=null;
		try {
			s = (String) jp.proceed();
			
			sb.append("{\"total\":"+p.getTotalCount()+",");
			
			sb.append("\"rows\":");
			sb.append(s);
			if(map.get(Utility.RETURN_KEY)!=null)
			{
				 if( map.get(Utility.RETURN_KEY).equals(Utility.RETURN_OK)){
					 sb.append(",").append(Utility.rtJson(Utility.RETURN_OK,"操作成功",null).substring(1));
				 }
				 else if(map.get(Utility.RETURN_KEY).equals(Utility.RETURN_ERROR)){
					 sb.append(",").append(Utility.rtJson(Utility.RETURN_OK,"操作异常",null).substring(1));
				 }
				 else{
					 sb.append(",").append(Utility.rtJson(Utility.RETURN_OK,"操作成功",null).substring(1));	 
				 }
			}
			
		} catch (Throwable e) {
			e.printStackTrace();
		}
    	return sb.toString();
    } 
    	  
    

}
